package com.bookstore.backend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookstore.backend.exception.ServiceException;
import com.bookstore.backend.mapper.OrderMapper;
import com.bookstore.backend.model.entity.Order;
import com.bookstore.backend.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: world
 * @date: 2022/7/29 15:43
 * @description: Book表的实现类
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询列表
     * @return
     */
    @Override
    public List<Order> orderSelect(Integer orderId) {
        List<Order>result = orderMapper.orderSelect(orderId);
        if (result.isEmpty()){
            throw new ServiceException("订单不存在!");
        }
        return result;
    }
    @Override
    public Integer orderinsert(Order order) {
        if (order.getOrderNo() == null){
            throw new ServiceException("订单编号不能为空");
        }
        if (order.getSubmitTime() == null){
            throw new ServiceException("提交时间不能为空");
        }
        if (order.getUsername() == null){
            throw new ServiceException("用户名不能为空");
        }
        if (order.getOrderAmount() == null){
            throw new ServiceException("订单金额不能为空");
        }
        if (order.getPaymentStatus() == null){
            throw new ServiceException("支付状态不能为空");
        }
        if (order.getOrderStatus() == null){
            throw new ServiceException("订单状态不能为空");
        }
        return orderMapper.orderinsert(order);
    }

    @Override
    public Integer updateNumByorderId(Order order) {
        return orderMapper.updateNumByorderId(order);
    }

    @Override
    public Integer deleteNumByorderId(Integer orderId) {
        Integer integer = orderMapper.deleteNumByorderId(orderId);
        if (integer == 0){
            throw new ServiceException("请选择订单");
        }
        return integer;
    }

    public IPage<Order> selectOrderPage(Page<Order> page, Integer size){
        return orderMapper.selectPageVO(page,size);
    }

}
